/**
 *  axios 封装
*/

import axios from 'axios'

export const request = axios.create({
  baseURL: 'https://conduit.productionready.io'
})

// 插件
//通过插件机制获取到上下文对象（query.params、req、res、app、store ...)

export default ({ store }) => {
  // http request 拦截器
  request.interceptors.request.use(
    config => {

      const { user } = store.state
      if (user && user.token) {
        config.headers.Authorization = `Token ${user.token}`  //请求头加上token
      }
      return config
    },
    err => {
      return Promise.reject(err)
    })
}